//
//  LGPayWayViewController.m
//  LuxuryGoods
//
//  Created by 刘星辰 on 2018/5/29.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "LGPayWayViewController.h"
#import "UIButton+Gradual.h"
#import "LGPayWayCell.h"
#import "LGInstallmentPayCell.h"

@interface LGPayWayViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *priceLab;
@property (weak, nonatomic) IBOutlet UIButton *payBtn;

/**
 是否折叠分期选项
 */
@property (assign,nonatomic) BOOL isShow;

@end

@implementation LGPayWayViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        self.isHaveInstalment = YES;
        self.isShow = NO;
    }
    return self;
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)])
    {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])
    {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"选择支付方式";
    
    [self.payBtn addGradualColor];
    self.view.backgroundColor = WHITE_COLOR;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.backgroundColor = BACKGROUND_COLOR;
    self.tableView.separatorColor = BACKGROUND_COLOR;
    self.tableView.allowsSelection = YES;
    
    WeakObj(self);
    [self.payBtn bk_addEventHandler:^(id sender) {
        
        if(selfWeak.tableView.indexPathForSelectedRow == nil)
        {
            [SVProgressHUD showInfoWithStatus:@"选择一个付款方式~"];
            return ;
        }
        
    } forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark -
#pragma mark tableView的Delegate和DataSource
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return (section == 0)?(CGFLOAT_MIN):(63);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    if (section == 0)
    {
        return view;
    }
    else
    {
        view.size = CGSizeMake(SCREEN_WIDTH, 63);
        view.backgroundColor = WHITE_COLOR;
        
        UIImageView * icon = [[UIImageView alloc] init];
        icon.frame = CGRectMake(0, 0, 23, 17);
        icon.image = [UIImage imageNamed:@"zhifu_zhifubao"];
        icon.userInteractionEnabled = YES;
        [view addSubview:icon];
        [icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(15);
            make.size.mas_equalTo(icon.size);
            make.centerY.equalTo(view);
            
        }];
        
        UILabel * label =  [UILabel new];
        label.text = @"分期支付";
        label.font = FONT(17);
        label.textColor = TEXT_COLOR_1;
        label.textAlignment = NSTextAlignmentLeft;
        label.numberOfLines = 0;
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(icon.mas_right).offset(24);
            make.centerY.equalTo(view);
        }];
    
        UIImageView * arrows = [[UIImageView alloc] init];
        arrows.frame = CGRectMake(0, 0, 8, 13);
        arrows.image = [UIImage imageNamed:@"dingdan_xiao_jiantou"];
        [view addSubview:arrows];
        [arrows mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(view.mas_right).offset(-15);
            make.size.mas_equalTo(arrows.size);
            make.centerY.equalTo(view);
        }];
        
        WeakObj(self);
        [view bk_whenTapped:^{
            selfWeak.isShow = !selfWeak.isShow;
            [selfWeak.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
            if (selfWeak.isShow)
            {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.31 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [selfWeak.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:NO];
                }); 
            }
        }];
        
        return view;
    
    }
    
    
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return (self.isHaveInstalment)?(2):(1);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger row = (self.isShow)?(4):(0);
    
    return (section == 0)?(4):(row);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        //创建xib复用单元格
        static NSString * ID = @"LGPayWayCell";
        LGPayWayCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"LGPayWayCell" owner:self options:nil] firstObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
    else
    {
        //创建xib复用单元格
        static NSString * ID = @"LGInstallmentPayCell";
        LGInstallmentPayCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"LGInstallmentPayCell" owner:self options:nil] firstObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
}



//实际高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 63;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat
{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}


- (void)dealloc
{
        [self.tableView bk_removeObserversWithIdentifier:@"tableView"];
}


@end
